package com.fireline.beauty.cashier;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 现金付款窗口
 * */
public class XjfkDialog extends Dialog {

	protected int result;
	protected Shell shell;
	private Text textFkje;
	private String yfje;
	private Text textZlje;
	private Text textJfje;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public XjfkDialog(Shell parent, String yfje) {
		super(parent, SWT.NONE);
		this.yfje = yfje;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result SWT.OK|SWT.CANCEL
	 */
	public int open() {
		createContents();
		Display display = getParent().getDisplay();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(418, 262);
		shell.setText(getText());
		shell.setLayout(new GridLayout(1, false));

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 20;
		gl_composite.marginWidth = 50;
		gl_composite.marginHeight = 20;
		composite.setLayout(gl_composite);

		Label label = new Label(composite, SWT.NONE);
		label.setText("交费金额");
		label.setFont(SWTResourceManager.getFont("华文细黑", 14, SWT.BOLD));

		textJfje = new Text(composite, SWT.READ_ONLY);
		textJfje.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				textJfje.redraw();
			}
		});
		textJfje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		textJfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textJfje.setFont(SWTResourceManager.getFont("宋体", 20, SWT.BOLD));
		textJfje.setText(yfje);
		ControlUtil.setCurrencyRule(textJfje, false, false);
		ControlUtil.setEnterToFocusMove(textJfje);

		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_2.setText("付款金额");
		label_2.setFont(SWTResourceManager.getFont("华文细黑", 14, SWT.BOLD));

		textFkje = new Text(composite, SWT.BORDER);
		textFkje.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				doInputIng();
			}
		});
		textFkje.setFocus();
		textFkje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textFkje.setFont(SWTResourceManager.getFont("幼圆", 22, SWT.BOLD));
		textFkje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		ControlUtil.setCurrencyRule(textFkje, false, false);
		ControlUtil.setEnterToFocusMove(textFkje);
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setText("找零金额");
		label_3.setFont(SWTResourceManager.getFont("华文细黑", 14, SWT.BOLD));

		textZlje = new Text(composite, SWT.READ_ONLY);
		textZlje.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				textZlje.redraw();
			}
		});
		textZlje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		textZlje.setText("");
		textZlje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textZlje.setFont(SWTResourceManager.getFont("幼圆", 22, SWT.BOLD));
		ControlUtil.setCurrencyRule(textZlje, false, false);
		ControlUtil.setEnterToFocusMove(textZlje);
		Label label_5 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOkPressed();
			}
		});
		btnNewButton.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton.setText("确定");

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doCancelPressed();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(70, SWT.DEFAULT));
		btnNewButton_1.setText("取消");

	}

	protected void doCancelPressed() {
		result = SWT.CANCEL;
		shell.close();
	}

	protected void doOkPressed() {
		if (new BigDecimal(textFkje.getText()).compareTo(new BigDecimal(yfje)) == -1) {
			MessageDialog.openInformation(shell, "提示信息", "付款金额不能小于应收金额");
			return;
		}
		result = SWT.OK;
		shell.close();
	}

	protected void doInputIng() {
		BigDecimal zlje = new BigDecimal(textFkje.getText())
				.subtract(new BigDecimal(yfje));
		textZlje.setText(zlje.toString());
	}

}
